home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / games / sokosrc.zoo / sok.c < prev    next >
C/C++ Source or Header  |  1991-01-26  |  6KB  |  212 lines

  1. #include <stdio.h>
  2. #include <curses.h>
  3. #include <string.h>
  4. #include "sokoban.h"
  5.  
  6. short level, packets, savepack, moves, pushes, rows, cols;
  7. short scorelevel, scoremoves, scorepushes;
  8. char  map[MAXROW+1][MAXCOL+1];
  9. POS   ppos;
  10. char  *username, *prgname;
  11.  
  12. short optshowscore = 0, optmakescore = 0, optrestore = 0, optlevel = 0;
  13. short scoring = 1, superuser = 0;
  14. short userlevel;
  15. extern int state_num;
  16.  
  17. #if ATARIST
  18. extern char *getenv();
  19.  
  20. char *getlogin()
  21. {
  22. static char username[40];
  23. char *c;
  24.     if ((c = getenv("USER")) != NULL) return c;
  25.  
  26.     printf("What is your name? ");
  27.     gets(username);
  28.     return username;
  29. }
  30. #else
  31.     char *getlogin();
  32. #endif
  33.  
  34. main( argc, argv)
  35. short argc;
  36. char *argv[];
  37. {
  38.    short ret, ret2;
  39.    char *strrchr();
  40.  
  41.    scorelevel = 0;
  42.    moves = pushes = packets = savepack = 0;
  43. #if ATARIST
  44.    if( (prgname = strrchr( argv[0], '\\')) == NULL)
  45. #else
  46.    if( (prgname = strrchr( argv[0], '/')) == NULL)
  47. #endif
  48.       prgname = argv[0];
  49.    else prgname++;
  50.  
  51.    if( (username = getlogin("SOKNAM")) == NULL)
  52.       ret = E_NOUSER;
  53.    else {
  54. #if ATARIST
  55.     superuser = TRUE;
  56. #else
  57.       superuser = (strcmp( username, SUPERUSER) == 0);
  58. #endif
  59.     state_num = 0;
  60.       if( (ret = checkcmdline( argc, argv)) == 0) {
  61.          if( optshowscore)
  62.            ret = outputscore();
  63.          else if( optmakescore) {
  64.            if( superuser) {
  65.               if( (ret = getpassword()) == 0)
  66.                  ret = makenewscore();
  67.            }
  68.            else ret = E_NOSUPER;
  69.         }
  70.         else if( optrestore) {
  71.            ret = restoregame();
  72.         }
  73.          else if( (ret = getuserlevel( &userlevel)) == 0) {
  74.             if( optlevel > 0) {
  75.               if( superuser) {
  76.                  level = optlevel;
  77.                  scoring = 0;
  78.               }
  79.               else if( userlevel < optlevel)
  80.                  ret = E_LEVELTOOHIGH;
  81.               else level = optlevel;
  82.            }
  83.            else level = userlevel;
  84.          }
  85.       }
  86.    }
  87.    if( ret == 0)
  88.       ret = gameloop();
  89.    errmess( ret);
  90.    if( scorelevel && scoring) {
  91.       ret2 = score();
  92.       errmess( ret2);
  93.    }
  94.    exit( ret);
  95. }
  96.  
  97. checkcmdline( argc, argv)
  98. short argc;
  99. char *argv[];
  100. {
  101.    short ret = 0;
  102.  
  103.    if( argc > 1)
  104.       if( (argc == 2) && (argv[1][0] == '-')) {
  105.         if( (argv[1][1] == 's') && (argv[1][2] == '\0'))
  106.            optshowscore = 1;
  107.         else if( (argv[1][1] == 'c') && (argv[1][2] == '\0'))
  108.            optmakescore = 1;
  109.         else if( (argv[1][1] == 'r') && (argv[1][2] == '\0'))
  110.            optrestore = 1;
  111.         else if( (level = optlevel = atoi( &(argv[1][1]))) == 0)
  112.            ret = E_USAGE;
  113.       }
  114.       else ret = E_USAGE;
  115.    return( ret);
  116. }
  117.  
  118. gameloop() {
  119.  
  120.    short ret = 0;
  121.  
  122.    initscr(); crmode(); noecho();
  123.    if( ! optrestore) ret = readscreen();
  124.    while( ret == 0) {
  125.       if( (ret = play()) == 0) {
  126.          level++;
  127.          moves = pushes = packets = savepack = 0;
  128.          ret = readscreen();
  129.          state_num = 0;
  130.       }
  131.    }
  132.    move( 0, 0); clrtobot(); refresh();
  133.    nocrmode(); echo(); erase(); endwin();
  134.    return( ret);
  135. }
  136.  
  137. getpassword() {
  138. #if ATARIST
  139. char c;
  140.     printf("Do you really want to make a new score file? ");
  141.     c = getchar();
  142.     if (c == 'y' || c == 'Y') return 0;
  143.     return E_ILLPASSWORD;
  144. #else
  145.    short c, i;
  146.    char *p, *getpass();
  147.  
  148.    p = getpass( "Password: ");
  149.    return( (strcmp( p, PASSWORD) == 0) ? 0 : E_ILLPASSWORD);
  150. #endif
  151. }
  152.  
  153. char *message[] = {
  154.    "illegal error number",
  155.    "cannot open screen file",
  156.    "more than one player position in screen file",
  157.    "illegal char in screen file",
  158.    "no player position in screenfile",
  159.    "too many rows in screen file",
  160.    "too many columns in screenfile",
  161.    "quit the game",
  162.    "cannot open the help file",
  163.    "cannot get your username",
  164.    "cannot open savefile",
  165.    "error writing to savefile",
  166.    "cannot stat savefile",
  167.    "error reading savefile",
  168.    "cannot restore, your savefile has been altered",
  169.    "game saved",
  170.    "too much users in score table",
  171.    "cannot open score file",
  172.    "error reading scorefile",
  173.    "error writing scorefile",
  174.    "illegal command line syntax",
  175.    "illegal password",
  176.    "level number too big in command line",
  177.    "only superuser is allowed to make a new score table",
  178.    "cannot find file to restore"
  179. };
  180.  
  181. errmess( ret)
  182. short ret;
  183. {
  184.    if( ret != E_ENDGAME) {
  185.       fprintf( stderr, "%s: ", prgname);
  186.       switch( ret) {
  187.          case E_FOPENSCREEN: case E_PLAYPOS1:   case E_ILLCHAR:
  188.         case E_PLAYPOS2:    case E_TOMUCHROWS: case E_TOMUCHCOLS:
  189.         case E_ENDGAME:     case E_FOPENHELP:  case E_NOUSER:
  190.         case E_FOPENSAVE:   case E_WRITESAVE:  case E_STATSAVE:
  191.         case E_READSAVE:    case E_ALTERSAVE:  case E_SAVED:
  192.         case E_TOMUCHSE:    case E_FOPENSCORE: case E_READSCORE:
  193.         case E_WRITESCORE:  case E_USAGE:      case E_ILLPASSWORD:
  194.         case E_LEVELTOOHIGH: case E_NOSUPER:   case E_NOSAVEFILE:
  195.                             fprintf( stderr, "%s\n", message[ret]);
  196.                              break;
  197.          default:            fprintf( stderr, "%s\n", message[0]);
  198.                              break;
  199.       }
  200.       if( ret == E_USAGE) usage();
  201.    }
  202. }
  203.  
  204. usage() {
  205.  
  206.    fprintf( stderr, "Usage: %s [-{s|c|r|<nn>}]\n\n", prgname);
  207.    fprintf( stderr, "           -c:    create new score table (superuser only)\n");
  208.    fprintf( stderr, "           -r:    restore saved game\n");
  209.    fprintf( stderr, "           -s:    show score table\n");
  210.    fprintf( stderr, "           -<nn>: play this level (<nn> must be greater 0)\n");
  211. }
  212.